package de.bytecake.snoobud.bootstrap.config;

import de.bytecake.snoobud.bootstrap.logging.ApplicationLogger;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Writer;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

/**
 * Provides a standard configuration if no config.xml file is found
 * @author Dirk
 */
public class StandardConfig {
    
    Logger logger = ApplicationLogger.getAppLogger();

    public StandardConfig() {
        init();
    }

    private void init() {
        try {
            Document doc = new Document();
            Element root = new Element("snoobud");
            doc.setRootElement(root);
            Element derby = new Element("derby");
            derby.setAttribute("dir", "/derby");
            root.addContent(derby);
            XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
            String configFile = System.getProperty("user.dir")+"/cconfig.xml";
            output.output(doc, new FileOutputStream(new File(configFile)));
            logger.info("A new config.xml has been written to: "+ configFile);
        } catch (Exception e) {
            logger.fatal("Error creating config.xml!", e);
            ConfigNotWriteablePanel panel = new ConfigNotWriteablePanel();
            NotifyDescriptor nd = new NotifyDescriptor(panel, "Title", NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.ERROR_MESSAGE, null, NotifyDescriptor.OK_OPTION);
            if(DialogDisplayer.getDefault().notify(nd)== NotifyDescriptor.OK_OPTION){
                System.exit(1);
            } else {
                System.exit(1);
            }
        }

    }
}
